package com.moli.iov.down.service.impl;

import com.moli.iov.down.model.dto.req.VehicleCommandQueryDto;
import com.moli.iov.down.model.po.VehicleCommand;
import com.moli.iov.down.repositories.VehicleCommandDao;
import com.moli.iov.down.service.IVehicleCommandService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.util.StringUtil;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 车辆命令业务
 * </p>
 *
 * @author zql
 * @since 2019/9/17 9:52
 */
@Slf4j
@Service("vehicleCommandService")
public class VehicleCommandServiceImpl implements IVehicleCommandService {

  @Autowired
  private VehicleCommandDao vehicleCommandDao;

  /**
   * 批量保存下发命令日志
   * @param vehicleCommands
   */
  @Override
  public void batchSaveCommand(List<VehicleCommand> vehicleCommands) {
    vehicleCommands.forEach(vehicleCommand -> saveCommand(vehicleCommand));
  }

  /**
   * 保存下发命令日志
   * @param vehicleCommand
   * @return
   */
  @Override
  public void saveCommand(VehicleCommand vehicleCommand) {
    vehicleCommandDao.save(vehicleCommand);
  }

  /**
   * 更新响应结果
   * @param vehicleCommand
   */
  @Override
  public void updateResult(VehicleCommand vehicleCommand) {
    Criteria criteria = new Criteria();
    criteria.and("vin").is(vehicleCommand.getVin());
    criteria.and("commandNo").is(vehicleCommand.getResponseNo());

    Update update = new Update()
        .set("resultMemo", vehicleCommand.getResultMemo());
    if(vehicleCommand.getStatus() != null){
      update.set("status", vehicleCommand.getStatus());
    }
    if(vehicleCommand.getResult() != null){
      update.set("result", vehicleCommand.getResult());
    }

    if(vehicleCommand.getResponseTime() != null){
      update.set("responseTime", vehicleCommand.getResponseTime());
    }
    vehicleCommandDao.update(new Query(criteria), update);
  }

  /**
   * 命令日志查询
   * @param vehicleCommandQueryDto
   * @return
   */
  @Override
  public PageExtend<VehicleCommand> listVehicleCommand(VehicleCommandQueryDto vehicleCommandQueryDto) {
    PageExtend<VehicleCommand> page = new PageExtend<>();
    page.setPageNumber(vehicleCommandQueryDto.getPageNum());
    page.setPageSize(vehicleCommandQueryDto.getPageSize());

    return vehicleCommandDao.findPage(page, assemblyConditions(vehicleCommandQueryDto));
  }

  /**
   * 获取单条记录
   * @param vehicleCommandQueryDto
   * @return
   */
  @Override
  public VehicleCommand getVehicleCommand(VehicleCommandQueryDto vehicleCommandQueryDto) {
    List<VehicleCommand> vehicleCommands = vehicleCommandDao
        .find(assemblyConditions(vehicleCommandQueryDto));

    if(vehicleCommands == null || vehicleCommands.isEmpty()){
      return null;
    }
    return vehicleCommands.get(0);
  }

  /**
   * 组装条件
   * @param vehicleCommandQueryDto
   * @return
   */
  private Query assemblyConditions(VehicleCommandQueryDto vehicleCommandQueryDto) {

    Criteria criteria = new Criteria();

    if(vehicleCommandQueryDto == null){
      return new Query(criteria);
    }

    if (StringUtil.isNotBlank(vehicleCommandQueryDto.getId())) {
      criteria.and("id").is(vehicleCommandQueryDto.getId());
    }else if(vehicleCommandQueryDto.getIds() != null && !vehicleCommandQueryDto.getIds().isEmpty()){
      criteria.and("id").in(vehicleCommandQueryDto.getIds());
    }

    if (StringUtil.isNotBlank(vehicleCommandQueryDto.getVin())) {
      criteria.and("vin").is(vehicleCommandQueryDto.getVin());
    }

    if(StringUtil.isNotBlank(vehicleCommandQueryDto.getLicense())){
      criteria.and("license").is(vehicleCommandQueryDto.getLicense());
    }

    if(vehicleCommandQueryDto.getResult() != null){
      criteria.and("result").is(vehicleCommandQueryDto.getResult());
    }

    if (vehicleCommandQueryDto.getBeginTime() != null &&
        vehicleCommandQueryDto.getEndTime() != null) {
      criteria.and("createTime")
          .gte(vehicleCommandQueryDto.getBeginTime())
          .lte(vehicleCommandQueryDto.getEndTime());
    } else if (vehicleCommandQueryDto.getBeginTime() != null) {
      criteria.and("createTime").gte(vehicleCommandQueryDto.getBeginTime());
    } else if (vehicleCommandQueryDto.getEndTime() != null) {
      criteria.and("createTime").lte(vehicleCommandQueryDto.getEndTime());
    }

    if(vehicleCommandQueryDto.getOperateTypes() != null &&
        !vehicleCommandQueryDto.getOperateTypes().isEmpty()){
      criteria.and("operateType").in(vehicleCommandQueryDto.getOperateTypes());
    }

    if(vehicleCommandQueryDto.getCompanyId() != null){
      criteria.and("companyId").is(vehicleCommandQueryDto.getCompanyId());
    }
    Query query = new Query(criteria);
    query.with(new Sort(Sort.Direction.DESC, "createTime"));
    log.info("入参：{} 组装条件：{}，", vehicleCommandQueryDto, query);
    return query;
  }
}
